<?php

class ContactsController extends Controller
{
	var $pageTitle = 'Автодинамика - Контакты';
	var $_model;
	var $page_id = 'page5';

	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'ext.captcha.MyCCaptchaAction',
				'backColor'=>0x111111,
				'foreColor'=>0xffffff,
				'maxLength'=> 10,
 				'minLength'=> 2,
				'testLimit'=> 0,
				'width'=>80,
			),
		);
	}
	
	public function actionIndex()
	{
	
		$part = Part::model()->find('alias=:alias', array(':alias'=>'contacts'));
		if(!empty($part->page_title))
			$this->pageTitle = $part->page_title;
		
		if(!empty($part->page_description))		
			$this->pageDescription = $part->page_description;
			
		if(!empty($part->page_keywords))
			$this->pageKeyWords = $part->page_keywords;
	
		if(isset($_POST['MessageForm'])){
			$message = new MessageForm();
			$message->attributes = $_POST['MessageForm'];
			if($message->validate()) {
				$body = '';
				$body .= 'Name: '.@$message->name."\n";
				$body .= 'E-mail: '.@$message->email."\n";
				$body .= 'Fax: '.@$message->fax."\n\n\n";
				$body .= 'Message: '.@$message->body."\n\n\n";
				mail(Yii::app()->params['email'], 'washservice.ru', $body);

				Yii::app()->user->setFlash('message', 'Ваше сообщение отправленно.', false);
			} else {
				Yii::app()->user->setFlash('message', 'Вы ошиблись при заполнении формы, попробуйте еще раз.', false);
			}

			
		/*
			if(!empty($_POST['Message']['name']) && !empty($_POST['Message']['email']) && !empty($_POST['Message']['text'])
			&& $_POST['Message']['text'] !== 'Сообщение:' && $_POST['Message']['name'] !== 'Имя:' && $_POST['Message']['email'] !== 'E-mail:' ) {

			}
		*/
		}
		
		$criteria=new CDbCriteria(array(
			'condition'=>'part_id='.Part::getByAlias('contacts')->id,
			'order'=>'created',
		));
	 
		$dataProvider=new CActiveDataProvider('Page', array(
			'pagination'=>false,
			'criteria'=>$criteria,
		));
	 
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	
	public function actionView(){
		
		$model = $this->loadModel();
		
		$this->render('view', array('model'=>$model));
	}
	
	private function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['alias']))
				$this->_model=Page::model()->find('alias=:alias', array(':alias'=>$_GET['alias']));
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}

	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}